﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace SpriteMaker
{
    public partial class AnimPlayerPanel : Control
    {
        //private Image texture;
        private int xCoord, yCoord;
        private float zoom;
        private int lastMouseX, lastMouseY;
        private Anim activeAnim;
        private AnimFrame activeAnimFrame;
        public formMain formInstance;
        System.Timers.Timer timer = new System.Timers.Timer();
        long playTime = 0;
        long animTime = 0;
        //int tickTime = 33;
        int currentFrame = 0;
        enum PlayerState
        {
            Stop,
            Play,
            Pause
        }
        PlayerState state = PlayerState.Stop;
        public AnimPlayerPanel()
        {
            InitializeComponent();
            timer.Interval = 100;
            timer.AutoReset = false;
            timer.Elapsed += new ElapsedEventHandler(Update);
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            pe.Graphics.Clear(Color.Gray);
            if (activeAnim != null && formInstance.GetImage() != null && activeAnim.animFrameList.Count > 0)
            {
                foreach (FrameModule frameModule in activeAnim.animFrameList[currentFrame].frame.frameModuleList)
                {
                    pe.Graphics.DrawImage(formInstance.GetImage()
                    , new Rectangle(frameModule.x + xCoord, frameModule.y + yCoord, frameModule.module.width, frameModule.module.height)
                        , new Rectangle(frameModule.module.x, frameModule.module.y, frameModule.module.width, frameModule.module.height), GraphicsUnit.Pixel);
                }
            }
        }
        void Update(object source, ElapsedEventArgs e)
        {
            if (currentFrame < activeAnim.animFrameList.Count - 1)
            {
                currentFrame++;
                if (currentFrame == activeAnim.animFrameList.Count - 1)
                {
                    if (activeAnim.loop)
                    {
                        timer.Interval = activeAnim.animFrameList[currentFrame].time;
                        timer.Start();
                    }
                }
                else
                {
                    timer.Interval = activeAnim.animFrameList[currentFrame].time;
                    timer.Start();
                }
            }
            else if (activeAnim.loop && activeAnim.animFrameList.Count > 1)
            {
                currentFrame = 0;
                timer.Interval = activeAnim.animFrameList[currentFrame].time;
                timer.Start();
            }
            Invalidate();
        }
        public void SetActiveAnim(Anim anim)
        {
            activeAnim = anim;
            playTime = 0;
            animTime = 0;
            if (anim != null)
            {
                foreach (AnimFrame animFrame in activeAnim.animFrameList)
                {
                    animTime += animFrame.time;
                }
            }
            this.Refresh();
        }
        public Anim GetActiveAnim()
        {
            return activeAnim;
        }
        public void SetActiveAnimFrame(AnimFrame animFrame)
        {
            activeAnimFrame = animFrame;
        }
        public AnimFrame GetActiveAnimFrame()
        {
            return activeAnimFrame;
        }
        public void Play()
        {
            if (state == PlayerState.Play || activeAnim == null)
            {
                return;
            }
            timer.Interval = activeAnim.animFrameList[currentFrame].time;
            state = PlayerState.Play;
            timer.Start();
        }
        public void Pause()
        {
            if (state == PlayerState.Pause || activeAnim == null)
            {
                return;
            }
            timer.Stop();
            state = PlayerState.Pause;
        }
        public void Stop()
        {
            if (state == PlayerState.Stop || activeAnim == null)
            {
                return;
            }
            timer.Stop();
            state = PlayerState.Stop;
            playTime = 0;
        }

        private void AnimPlayerPanel_SizeChanged(object sender, EventArgs e)
        {
            xCoord = this.ClientSize.Width / 2;
            yCoord = this.ClientSize.Height / 2;
        }
    }
}
